import { Toast } from 'vant'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/home/index.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: "首页",
      needCheck: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue'),
    meta: {
      title: "登录",
      needCheck: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/login/register.vue'),
    meta: {
      title: "注册",
      needCheck: false
    }
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('../views/category/index.vue'),
    meta: {
      title: "分类",
      needCheck: false
    }
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/cart/index.vue'),
    meta: {
      title: "购物车",
      needCheck: true
    }
  },
  {
    path: '/orderPreview',
    name: 'orderPreview',
    component: () => import('../views/cart/orderPreview.vue'),
    meta: {
      title: "订单预览",
      needCheck: true
    }
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/my/index.vue'),
    meta: {
      title: "我的",
      needCheck: true
    }
  },
  {
    path: '/orders',
    name: 'orders',
    component: () => import('../views/my/orders.vue'),
    meta: {
      title: "我的订单",
      needCheck: true
    }
  },
  {
    path: '/orderDetail/:id',
    name: 'orderDetail',
    component: () => import('../views/my/orderDetail.vue'),
    meta: {
      title: "订单详情",
      needCheck: true
    }
  },
  {
    path: '/collect',
    name: 'collect',
    component: () => import('../views/my/collect.vue'),
    meta: {
      title: "我的收藏",
      needCheck: true
    }
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import('../views/my/setting.vue'),
    meta: {
      title: "账号管理",
      needCheck: true
    }
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('../views/my/address/address.vue'),
    meta: {
      title: "地址管理",
      needCheck: true
    }
  },
  {
    path: '/address/update',
    name: 'addressUpdate',
    component: () => import('../views/my/address/update.vue'),
    meta: {
      title: "更新地址",
      needCheck: true
    }
  },
  {
    path: '/address/create',
    name: 'addressCreate',
    component: () => import('../views/my/address/create.vue'),
    meta: {
      title: "新增地址",
      needCheck: true
    }
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/my/about.vue'),
    meta: {
      title: "关于我们",
      needCheck: true
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('../views/public/detail.vue'),
    meta: {
      title: "详情页",
      needCheck: false
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const { title,needCheck } = to.meta
  // document.title = title
  const token = localStorage.getItem("bookshop-token")
  if(needCheck === true && !token){
    Toast("登录后才可访问!")
    next({ name: 'login' })
  }else{
    next()
  }
})

export default router
